import APIConfig from "../../../../config/api";
import cache from "../../../../enum/cache";
import Service from "../../../../model/service";
import FileUploader from "../../../../utils/file-uploader";
import { getDataSet } from "../../../../utils/util";

// pages/project-detail/components/third/third.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    taskId: String,
    managerId: String,
    deptType: String,
    tempId: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    thirdList: [],
    active: [0],
    radio: '1',
    show: false, // 驳回详情弹窗
    showBtn: false, //驳回展示按钮
    indexBtn: 0, // 驳回展示按钮下标
    value: '',
    file: {}, // 查看驳回原因
    files: []
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    ready() {
      console.log("3yemuian");
      this._getFileList()
    }
},
  methods: {
    // 折叠面板隐藏展示
    handleChange(e) {
      this.setData({
        active: e.detail
      })
    },
    // 点击文件预览
    handleClick(e) {
      const file = e.currentTarget.dataset.file
      const token = wx.getStorageSync(cache.TOKEN)
    wx.downloadFile({
      url: `${APIConfig.baseUrl}/minioApp/getMinioUrlOut?fileName=${file.previewName}&bucketName=cailiao`,
      header: {
        'Authorization': `Bearer ${token}`
      },
      success: (res) => {
        console.log(res);
        const filePath = res.tempFilePath
      wx.openDocument({
        filePath: filePath,
        fileType: 'pdf',
        success: (res) => {
          console.log(res);
        }
      })
    },
    fail: (err) => {
      console.log(err);
    }
    })
    },
    // 展示驳回详情
    async handleOpen(e) {
      let file = e.currentTarget.dataset.file
      console.log(file.id);
      let res = await Service.getFileDetail({fileId: file.id})
      console.log(res);
      this.setData({
        show: true,
        file: res.data
      })
    },
    // 驳回展示详情和上传文件
    handleShow(e) {
      this.setData({
        showBtn: !this.data.showBtn,
        indexBtn: getDataSet(e, 'index')
      })
    },
    // 关闭驳回详情
    onClose() {
      this.setData({
        show: false,
        file: {}
      })
    },
    // 获取文件清单
  async _getFileList() {
    let query = {
      taskId: this.data.taskId, // kns0L5WNYZ90NPxbXegMpZqnk5Uh4MFL
      tempParentId: this.data.tempId // 业务类型id
    }
    const {data} = await Service.getFileList(query)
    this.setData({
      thirdList: data
    })
  },
  // 新增 文件上传
  async handleUpload(e) {
    let file = getDataSet(e, 'file')
    
    const res = await wx.chooseMessageFile({
      count: 1,
      type: 'file',
  })

  let filePath = res.tempFiles[0].path
  let query = {
    fileId: file.bucketName ? file.id : 0,
    fileRealName: res.tempFiles[0].name, // 文件名
    bucketName: 'cailiao', // bucket名称
    projectId: file.taskId, // 项目ID
    ownerId: file.ownerId ? file.ownerId : file.id, //拥有者
    fileType: file.fileType, // 文件类型
    // isSingle: false, //是否为单文件上传
    reviewStatus: '1', // 审核状态 1材料 2预审
    fileSource: '1', // 文件来源 0意见反馈 1上传材料
  }
  
  let data = await FileUploader.uploadFile(filePath, query)
  console.log(data);
  this._getFileList()
  this.setData({
    showBtn: false,
    indexBtn: 0
  })
  },
  }
})